package cn.raven.basic.controller;

import cn.raven.basic.dto.LoginDto;
import cn.raven.basic.dto.PhoneLoginDto;
import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.service.ILoginService;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.user.dto.WxDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    
    /** 
    * @Title: account
    * @Description: 账户登录
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/8 12:04  
    * @Parameters: [loginDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginService.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试!");
        }
    }
    /**
    * @Title: phonein
    * @Description: 手机号登录
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/8 14:38
    * @Parameters: [loginDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/phonein")
    public AjaxResult phonein(@RequestBody PhoneLoginDto phoneLoginDto){
        try {
            Map<String,Object> map = loginService.phonein(phoneLoginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试!");
        }
    }

    
    /** 
    * @Title: phoneCode
    * @Description: 手机登录的手机验证码
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/8 13:29
    * @Parameters: [phoneLoginDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/phone/code")
    public AjaxResult phoneCode(@RequestBody PhoneLoginDto phoneLoginDto){
        try {
            loginService.phoneCode(phoneLoginDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试!");
        }
    }
    
    /** 
    * @Title: wechat
    * @Description: 微信扫码登录
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/10 15:18  
    * @Parameters: [wxDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WxDto wxDto){
        try {
            return loginService.wechat(wxDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试!");
        }
    }
    
    /** 
    * @Title: binder
    * @Description: 微信绑定账号
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/10 18:04
    * @Parameters: [wxDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WxDto wxDto){
        try {
            Map<String,Object> map = loginService.binder(wxDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试!");
        }
    }
}
